﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data.SqlClient;

namespace TARLib
{
    public class ComandaSimpleCollection : BindingList<ComandaSimple>
    {
        public static ComandaSimpleCollection OttieniComandePerLettera(int puntoDistribuzioneID, string lettera, DateTime currentDatetime)
        {
            ComandaSimpleCollection retValue = new ComandaSimpleCollection();

            SqlConnection conn = new SqlConnection(Database.GetDBConnectionString());
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "[Cucina].[SP_ComandePerLettera_CL]";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PuntoDistribuzioneID", System.Data.SqlDbType.Int, 20) { Value = puntoDistribuzioneID });
            cmd.Parameters.Add(new SqlParameter("@Lettera", System.Data.SqlDbType.NVarChar, 10) { Value = lettera });
            cmd.Parameters.Add(new SqlParameter("@Giorno", System.Data.SqlDbType.DateTime, 20) { Value = currentDatetime});

            SqlDataReader dr = null;
            try
            {
                conn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    retValue.Add(new ComandaSimple(
                        (int)dr["ComandaID"],
                        dr["CodiceComanda"].ToString(),
                        (bool)dr["IsCheckInRichiesto"],
                        (bool)dr["IsStampaRemotaRichiesta"],
                        (bool)dr["IsConsegnaRichiesta"],
                        (DateTime)dr["DataOraComanda"],
                        dr["DataOraEliminazione"] == DBNull.Value ? null : (DateTime?)dr["DataOraEliminazione"],
                        dr["DataOraCheckIn"] == DBNull.Value ? null : (DateTime?)dr["DataOraCheckIn"],
                        dr["DataOraStampaRemota"] == DBNull.Value ? null : (DateTime?)dr["DataOraStampaRemota"],
                        dr["DataOraConsegna"] == DBNull.Value ? null : (DateTime?)dr["DataOraConsegna"]));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                    dr.Dispose();
                }
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }

            return retValue;
        }
    }
}
